<template>
    <div class="loginBackground">
        <div class="loginForm mt20">
            <div class="contentCenter">
                <span>登录</span>
            </div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px">
                <el-form-item label="账号" prop="username">
                    <el-input type="text" v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div class="contentCenter">
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                <el-button type="primary" @click="toRegister()">注册</el-button>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'Login',
        data() {
            return {
                ruleForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        {required: true, message: '请输入账号'},
                        {min: 2, max: 32, message: '长度在 2 到 32 个字符'}
                    ],
                    password: [
                        {required: true, message: '请输入密码'},
                        {min: 3, max: 32, message: '长度在 3 到 32 个字符'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        this.loginUser(this.ruleForm);
                    } else {
                        return false;
                    }
                });
            },

            toRegister() {
                //路由跳转
                this.$router.push('/register');
            },

            async loginUser(data) {
                const res = await this.postJSON(this.$urlApi.login, data);
                console.log(res);
                //路由跳转
                this.$router.push('/home');
            }
        }
    };
</script>

<style scoped>
    .loginForm {
        width: 400px;
        min-width: 400px;
        clear: both;
    }

    .loginBackground {
        background: url('../assets/images/login.jpg');
        width: 100%;
        height: -webkit-fill-available;
        overflow: hidden;
        position: relative;
    }
</style>
